// 2014-02-01 Tyrghen
//---------------------------------------------
// Usage:
//  [] call edt_fnc_;
//---------------------------------------------
// Purpose:
// 
// 
// Parameters:
// - 
//
// Returns:
// 
//---------------------------------------------
// Objectif:
// 
//
// Paramètres:
// - 
//
// Renvoie:
// 
//---------------------------------------------

//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "editor\fn_boardVehicle.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_unit","_vec","_position","_pos_index","_objective","_jip_var","_cancel_board","_initial_pos"];
DEFAULT_PARAM(_unit,0,objNull);
DEFAULT_PARAM(_vec,1,objNull);
DEFAULT_PARAM(_position,2,"");
DEFAULT_PARAM(_objective,3,-1);
DEFAULT_PARAM(_pos_index,4,-1);

if (isNull _unit || isNull _vec) then {
	ERROR("Invalid Parameters");
} else {
	if (local _unit) then {
		_cancel_board = false;
		if (AND_ALSO(IS_SCALAR(_objective),(_objective >= 0))) then {
			_jip_var = format[TMF_EDITOR_OBJECTIVE,_objective];
			_cancel_board = [TMF_SERVER_INSTANCE_ID,_jip_var,false] call FFUNC(getMissionJIPVar);
		};
		
		if (!_cancel_board) then {
			sleep 0.1;
			_position = switch (toupper(_position)) do {
				case "COMMANDER": { "COMMANDER" };
				case "GUNNER": { "GUNNER" };
				case "CARGO": { "CARGO" };
				default { "DRIVER" };
			};
			_initial_pos = getPos _unit;
			_unit allowDamage false;
			TRACE_1("Position",ARR_5(_position,count(crew _vec),(isnull driver _vec),(isnull commander _vec),(isnull gunner _vec)));
			if (_position == "DRIVER" && (!isnull (driver _vec))) then { _position = "COMMANDER"; };
			if (_position == "COMMANDER" && (!isnull (commander _vec))) then { _position = "GUNNER"; };
			if (_position == "GUNNER" && (!isnull (gunner _vec))) then { _position = "CARGO"; };
			TRACE_1("Position",ARR_2(_position,_pos_index));
			switch (_position) do {
				case "COMMANDER": {
					_unit assignAsCommander _vec;
					_unit setPos (getPos _vec);
					[_unit] orderGetIn true;
					sleep 0.1;
					[_unit] orderGetIn true;
					sleep 0.1;
					if (vehicle _unit == _unit) then {
						_unit moveInCommander _vec;
					};
				};
				case "GUNNER": {
					_unit assignAsGunner _vec;
					_unit setPos (getPos _vec);
					[_unit] orderGetIn true;
					sleep 0.1;
					[_unit] orderGetIn true;
					sleep 0.1;
					if (vehicle _unit == _unit) then {
						_unit moveInGunner _vec;
					};
				};
				case "CARGO": {
					_unit assignAsCargo _vec;
					_unit setPos (getPos _vec);
					[_unit] orderGetIn true;
					sleep 0.1;
					[_unit] orderGetIn true;
					sleep 0.1;
					if (vehicle _unit == _unit) then {
						if (_pos_index < 0) then { _pos_index = count (assignedCargo _vec); };			
						_unit moveInCargo [_vec, _pos_index];
					};
				};
				case "DRIVER": {
					_unit assignAsDriver _vec;
					_unit setPos (getPos _vec);
					[_unit] orderGetIn true;
					sleep 0.1;
					[_unit] orderGetIn true;
					sleep 0.1;
					if (vehicle _unit == _unit) then {
						_unit moveInDriver _vec;
					};
				};
			};
			_wait = 20;
			while {(vehicle _unit == _unit) && _wait > 0} do { sleep 0.2; };
			// We check if it failed
			if (vehicle _unit == _unit) then {
				_unit setPos _initial_pos;
				[["STR_BOARDING_FAILED",([_vec] call FFUNC(displayName))],"unit",_unit,"global"] call FFUNC(handleMessage);
			};
			_unit allowDamage true;
		} else {
			TRACE("JIP Canceled");
		};
	} else {
		INFO("Unit is not local");
	};
};
TRACE("End");
TMFPROFILERSTOP;